package com.pencilisland.pay.mgt.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.app.entity.GoodsEntity;
import com.pencilisland.pay.mgt.app.service.GoodsService;

/**
 * 商品处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/app/goods")
public class GoodsController {
	@Autowired
	GoodsService goodsService;

	/**
	 * 进入商品管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String toTenantList() {
		return "app/goods";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> goodsList(@ModelAttribute GoodsEntity cretiria,
			@RequestParam(value = "start") int start, @RequestParam(value = "length") int length) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Page<GoodsEntity> goodsPage = goodsService.goodsList(cretiria, start, length);

		resultMap.put("recordsFiltered", goodsPage.getTotalElements());
		resultMap.put("recordsTotal", goodsPage.getTotalElements());
		resultMap.put("goodsList", goodsPage.getContent());

		return resultMap;
	}

	/**
	 * 保存更新
	 * 
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public GoodsEntity save(@ModelAttribute GoodsEntity goods) {
		return goodsService.save(goods);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", goodsService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			goodsService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/img/delete/{id}")
	@ResponseBody
	public Map<String, Object> deleteImg(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			goodsService.deleteImg(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/imgs/{goodsId}/{type}")
	@ResponseBody
	public Map<String, Object> imgs(@PathVariable(value = "goodsId") Long goodsId,@PathVariable(value = "type") Integer type) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("files", goodsService.imgs(goodsId,type));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/publish/{goodsId}")
	@ResponseBody
	public Map<String, Object> publish(@PathVariable(value = "goodsId") Long goodsId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			goodsService.publish(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/cancel/{goodsId}")
	@ResponseBody
	public Map<String, Object> cancel(@PathVariable(value = "goodsId") Long goodsId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			goodsService.cancel(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/qrcode/{goodsId}")
	@ResponseBody
	public Map<String, Object> qrcode(@PathVariable(value = "goodsId") Long goodsId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("qrcodeUrl", goodsService.qrcode(goodsId));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}
}
